package services;

import java.util.Iterator;
import java.util.LinkedList;

import objetos.ItemVendaObj;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;

import classes.CarrinhoCompraImpl;
import classes.CartaoFidelidadeImpl;

import java.lang.Boolean;

public class CarrinhoCompraService {

	public OMElement adicionarItem(OMElement element) {
		
		OMFactory fac = OMAbstractFactory.getOMFactory();
		OMNamespace ns_ret = fac.createOMNamespace("return", "ret");
		OMElement return_element = fac.createOMElement("atributo", ns_ret);
		
		CarrinhoCompraImpl cci = new CarrinhoCompraImpl();
		ItemVendaObj item = new ItemVendaObj();
				
		// Criar um OMElement que retorna um booleano
		Iterator<OMElement> it = element.getChildElements();
		OMElement item_par = it.next();
		OMElement clienteId_par = it.next();
		
		item.loadOMElement(item_par);
		
		if (cci.adicionarItem(item, Integer.parseInt(clienteId_par.getText())))
			return_element.setText("true");
		else return_element.setText("false");
		
		return return_element;
	}
	
	public OMElement removerItem(OMElement element) {
		// ItemVendaObj
		// int client
		
		// Elemento retorno
		OMFactory fac = OMAbstractFactory.getOMFactory();
		OMNamespace ns_ret = fac.createOMNamespace("return", "ret");
		OMElement return_element = fac.createOMElement("boolean", ns_ret);
		
		CarrinhoCompraImpl cci = new CarrinhoCompraImpl();
		
		Iterator<OMElement> it = element.getChildElements();
		OMElement item_par = it.next();
		OMElement clienteId_par = it.next();
		
		ItemVendaObj item = new ItemVendaObj();
		item.loadOMElement(item_par);
		
		int clienteId = Integer.parseInt(clienteId_par.getText());
		
		if (cci.removerItem(item, clienteId)) return_element.setText("true");
		else return_element.setText("false");
		
		return return_element;
		
	}
	
	public OMElement produtosNoCarrinho(OMElement element) {
		
		// Elemento retorno
		OMFactory fac = OMAbstractFactory.getOMFactory();
		OMNamespace ns_ret = fac.createOMNamespace("return", "ret");
		OMElement return_element = fac.createOMElement("lista", ns_ret);
		
		CarrinhoCompraImpl cci = new CarrinhoCompraImpl();
		
		LinkedList<ItemVendaObj> list = cci.produtosNoCarrinho(Integer.parseInt(element.getText()));
		Iterator<ItemVendaObj> it = list.iterator();
		
		while(it.hasNext())
			return_element.addChild(it.next().toOMElement());

		return return_element;
		
	}

	public OMElement limparCarrinho(OMElement element) {
		
		CarrinhoCompraImpl cci = new CarrinhoCompraImpl();
		
		Integer clienteId = Integer.parseInt(element.getText());
		
		Boolean retorno = cci.limparCarrinho(clienteId);
		
		element.setText(retorno.toString());
		
		return element;
	}
	
	public OMElement obterSomaDePontos(OMElement element) {
		
		CartaoFidelidadeImpl cfi = new CartaoFidelidadeImpl();
		
		Integer total_de_pontos = cfi.obterSomaDePontos(Integer.parseInt(element.getText()));
		
		element.setText(total_de_pontos.toString());
		
		return element;
		
	}
}
